﻿using Prism.Mvvm;
using Sharp.Infrastructure;
using StockTools.Configuration;
using StockTools.Model;
using StockTools.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace StockTools
{
    /// <summary>
    /// 配置文件实体类
    /// </summary>
    public class SettingConfig : ISystemConfig
    {
        public SettingConfig()
        {
            //默认值
            BuyBrokerageRatio = 2.5M;
            MinBrokerageRatio = 5M;
            StampDuty = 1M;
            MinStampDuty = 1M;
            TransferMoney = 1M;
            MinTransferMoney = 1M;
            StockData = new List<SettingConfig.StockItem>();
        }
        /// <summary>
        /// 股票代码
        /// </summary>
        public string Code { get; set; }
        public decimal BuyBrokerageRatio { get; set; }
        public decimal MinBrokerageRatio { get; set; }
        public decimal StampDuty { get; set; }
        public decimal MinStampDuty { get; set; }
        public decimal TransferMoney { get; set; }
        public decimal MinTransferMoney { get; set; }

        public List<SettingConfig.StockItem> StockData { get; set; }


        public string ConfigFileName => "settingConfig.json";

        public decimal BuyOtherMoney { get; set; }
        public decimal SellOtherMoney { get; set; }

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="code"></param>
        /// <param name="vm"></param>
        /// <param name="sds"></param>
        internal void Save(string code, RateSettingModel vm, IList<SettingConfig.StockItem> sds)
        {
            if (code == null && vm == null && sds == null)
                return;

            var settingConfig = SystemConfigManage.Read<SettingConfig>();

            if (!code.IsNullOrEmpty())
            {
                settingConfig.Code = code;
            }


            if (vm != null)
            {
                settingConfig.BuyBrokerageRatio = vm.BuyBrokerageRatio;
                settingConfig.MinBrokerageRatio = vm.MinBrokerageRatio;
                settingConfig.StampDuty = vm.StampDuty;
                settingConfig.MinStampDuty = vm.MinStampDuty;
                settingConfig.TransferMoney = vm.TransferMoney;
                settingConfig.MinTransferMoney = vm.MinTransferMoney;
                settingConfig.BuyOtherMoney = vm.BuyOtherMoney;
                settingConfig.SellOtherMoney = vm.SellOtherMoney;
            }
            if (sds == null)
            {
                sds = new List<SettingConfig.StockItem>();
            }
            settingConfig.StockData = sds.ToList();
             
            SystemConfigManage.Save(settingConfig);
        }



        public class StockItem : BindableBase
        {
            private decimal latestPrice;
            private decimal rate;
            private decimal highest;
            private decimal lowest;
            private bool isActive;
            private string name;

            [XmlIgnore]
            public bool IsActive { get => isActive; set => SetProperty(ref isActive, value); }
            public string Name { get => name; set => SetProperty(ref name, value); }
            public string Key { get; set; }
            /// <summary>
            /// 最新价格
            /// </summary>
            public decimal LatestPrice
            {
                get => latestPrice;
                set => SetProperty(ref latestPrice, value);
            }
            /// <summary>
            /// 涨幅
            /// </summary>
            [XmlIgnore]
            public decimal Rate { get => rate; set => SetProperty(ref rate, value); }
            /// <summary>
            /// 最高
            /// </summary>
            [XmlIgnore]
            public decimal Highest { get => highest; set => SetProperty(ref highest, value); }
            /// <summary>
            /// 最低
            /// </summary>
            [XmlIgnore]
            public decimal Lowest { get => lowest; set => SetProperty(ref lowest, value); }
        }
    }
}